﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace SchemesServer
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ServiceHost host = null;

        public MainWindow()
        {
            InitializeComponent();

            WCFServer.SchemesServer.Event_OnMessage += new Action<string, bool>(SchemesServer_Event_OnMessage);

            try
            {
                Txt_Sn.Text = Comm.HardDisk.HardDiskHelper.GetAllHardDiskSN();
            }
            catch (Exception ex)
            {
                this.WriteInfoToText(ex.Message, Colors.Red);
            }
            Btn_Start.Click += new RoutedEventHandler(Btn_Start_Click);
            Btn_Stop.Click += new RoutedEventHandler(Btn_Stop_Click);
            Btn_Clear.Click += new RoutedEventHandler(Btn_Clear_Click);
            if (App.IsLoadStart) Btn_Start_Click(Btn_Start, new RoutedEventArgs());
        }

        void SchemesServer_Event_OnMessage(string arg1, bool arg2)
        {
            WriteInfoToText(arg1, arg2 ? Colors.Red : Colors.Black);
        }

        private void Btn_Clear_Click(object sender, RoutedEventArgs e)
        {
            Txt_Log.Document.Blocks.Clear();
        }

        private void Btn_Stop_Click(object sender, RoutedEventArgs e)
        {
            if (host != null)
            {
                if (host.State != CommunicationState.Closed)
                {
                    WriteInfoToText("服务已经停止...", Colors.Black);
                    host.Close();
                }
                host = null;
                Btn_Start.IsEnabled = true;
            }
        }

        private void Btn_Start_Click(object sender, RoutedEventArgs e)
        {
            Txt_Log.Document.Blocks.Clear();

            try
            {
                host = new ServiceHost(typeof(SchemesServer.WCFServer.SchemesServer));

                host.Open();

                foreach (ServiceEndpoint s in host.Description.Endpoints)
                {
                    WriteInfoToText("服务启动成功，服务Uri路径：" + s.Address.Uri.ToString(), Colors.Black);
                }

                WriteInfoToText("按【关闭服务】按钮，停止服务...", Colors.Black);

                Btn_Start.IsEnabled = false;
            }
            catch (Exception ex)
            {
                WriteInfoToText(ex.Message, Colors.Red);
            }
        }


        private void WriteInfoToText(string message, Color color)
        {
            FlowDocument fd = Txt_Log.Document;

            if (fd.Blocks.Count > 3000) fd.Blocks.Remove(fd.Blocks.First());

            Paragraph p = new Paragraph();

            Run r = new Run(string.Format("【{0:yy-MM-dd HH:mm:ss}】:{1}", DateTime.Now, message));

            r.Foreground = new SolidColorBrush(color);

            p.Inlines.Add(r);

            fd.Blocks.Add(p);

        }



    }
}
